/**
 * 装置表管理js
 * 
 * 1、功能：查询 、删除
 * 2、
 * 
 */

window.onload = function(){

//BEGIN ———*————*———*————查询功能———*————*———*————*———*————*
	
//1、改servlet地址
	var sendServletAPI ="../device_FindServlet";
	
//查询装置表信息
	xmlRequest(sendServletAPI);
	
//获取元素
	var find = document.getElementById("find");
	var resT ="";
	
	//3、改表名
	var device="";
	
	//4、改表头信息，与列名
	var tableTitle = "<table>" +
			"<caption>装置表信息管理</caption>" +
				"<tr>" +
					"<th>选项</th>" +
					"<th>装置号</th>" +
					"<th>装置名</th>" +
					"<th>装置位置</th>" +
					"<th>饮品库存数（瓶）</th>" +
					"<th>医疗包库存（个）</th>" +
					"<th>体育用品库存（个）</th>"+
					"<th>操作</th>"+
				"</tr>";
	
	var tableTail="</table>";
	
	
//查询
	function xmlRequest(sendServletAPI){
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function(){
			if(xhr.readyState==4){
				if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
					var resT = xhr.responseText;
					//console.log("xhrResponse:获取数据库数据成功！");
					
					if(resT!="0"){
						//5、改JsonArray表名 eg:XXXArray 同下
						var deviceArray = JSON.parse(resT);
						//6、改JSON数据格式
						for(var i=0;i<deviceArray.length;i++){
							if(i%2==0){
								device+="<tr id='lightColor'>" +
											"<td><input type='checkbox' name='check' value='"+deviceArray[i].dId+"'/></td>" +
											"<td>"+deviceArray[i].dId+"</td>" +
											"<td>"+deviceArray[i].dName+"</td>" +
											"<td>"+deviceArray[i].dLocation+"</td>" +
											"<td>"+deviceArray[i].dDrink+"</td>" +
											"<td>"+deviceArray[i].dKit+"</td>" +
											"<td>"+deviceArray[i].dSupply+"</td>" +
											"<td><a href='javascript:link(\"../device_DeleteServlet?Ids=K "+deviceArray[i].dId+"\")'>删除</a></td>" +
										"</tr>";
							}else{
								device+="<tr>" +
										"<td><input type='checkbox' name='check' value='"+deviceArray[i].dId+"'/></td>" +
										"<td>"+deviceArray[i].dId+"</td>" +
										"<td>"+deviceArray[i].dName+"</td>" +
										"<td>"+deviceArray[i].dLocation+"</td>" +
										"<td>"+deviceArray[i].dDrink+"</td>" +
										"<td>"+deviceArray[i].dKit+"</td>" +
										"<td>"+deviceArray[i].dSupply+"</td>" +
										"<td><a href='javascript:link(\"../device_DeleteServlet?Ids=K "+deviceArray[i].dId+"\")'>删除</a></td>" +
									"</tr>";
							}
						}
						resT=tableTitle+device+tableTail;
						find.innerHTML=resT;
					}else{
						find.innerHTML = "<p>表信息空空如也--''</p>";
					}
				}else{
					find.innerHTML="<p>抱歉！查询信息失败！</p>";
					console.log("查询失败！");
				}
			}
		}
		
		xhr.open("post",sendServletAPI,true);
		xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
		xhr.send("find=allTables");
	}

//END  ———*————*———*————查询功能———*————*———*————*———*————*
	
//BEGIN  ———*————*———*————*删除功能———*————*———*————*———*————*
	
	var deleteItem = document.getElementById("delete");
	deleteItem.onclick = function(){
		deleteFunction();
	}
	
	//获取所选项Id
	function getCheckedId(){
		var checks = document.getElementsByName("check");
		var Ids="";
		for(var i=0;i<checks.length;i++){
			if(checks[i].checked){
				Ids+=checks[i].value+" ";
			}
		}
		console.log(Ids);
		return Ids;
	}
	
	//删除请求
	function deleteFunction(){
		var Ids = getCheckedId();
		//7、改动删除提示语：删除确定
		if(Ids!=""){
			var dlt = confirm("您确定要删除选中装置吗？");
			if(dlt){
				if(Ids!=""){
					//删除请求发送
					console.log("Ids："+Ids);
					var reAjax = deleteAjax(Ids);
					//删除结果提示
					if(reAjax!=0){
						alert("删除成功！");
						//隐藏删除功能区
					}else{
						alert("删除失败！");	
					}
					xmlRequest(sendServletAPI);
				}else{
					alert("请先选择您要删除的装置。");
				}
			}
		}
		else{
			alert("请先选择您要删除的装置。");
		}
	}
		
		
		//xhr发送获取到的id
		function deleteAjax(Ids){
			var xhr = new XMLHttpRequest();
			xhr.onreadystatechange = function(){
				if(xhr.state==4){
					if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
						var reAjax = xhr.response;
						if(reAjax!="0"){
							alert("删除成功！");
						}else{
							alert("删除失败！");
						}
					}else{
						console.log("删除失败！");
					}
				}
			}
			xhr.open("post","../device_DeleteServlet",true);
			xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
			xhr.send("Ids="+Ids);
		}
//事件区
	//全选/全反选事件
		var checkAll = document.getElementById("checkAll");
		checkAll.onclick = function(){
			var checks = document.getElementsByName("check");
			for(var i=0;i<checks.length;i++){
				checks[i].checked=!checks[i].checked;
			}
			checkAll.checked=!deleteBatch.checked;
		}
		
	//隐藏按钮事件
		var hiddenButtons = document.getElementById("hiddenButtons");
		
		var deleteBatch = document.getElementById("deleteBatch");
		deleteBatch.onclick = function(){
			hiddenButtons.style.visibility="visible";
		}
		
		
		var cancel = document.getElementById("cancel");
		cancel.onclick = function(){
			hiddenButtons.style.visibility="hidden";
		}
		
		
}